<?php
namespace App\Http\Controllers\Api;
use Log;

class HttpCurlController extends Controller
{
    public $result = false;
    public $errno = 0;
    public $errmsg = "";
    public $response = "";
    public $resp_code = 0;
    public $cacert = null;
    private $header = array();

    public function add_header($header) {
        $this->header = $header;
    }

    public function get($url, $timeout)
    {
        $ssl = $this->checkSSL($url);

        $this->init();
        Log::info("get request: [$url]");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);

    if ($ssl)
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }

    if ($this->cacert) {
         curl_setopt($curl, CURLOPT_CAINFO, $this->cacert);
    }
        curl_setopt($curl, CURLOPT_HEADER, 0);

    if (!empty($this->header))
        curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);

    $this->result = false;
    $this->response = curl_exec($curl);
    $this->errno = curl_errno($curl);
    if ($this->errno > 0)
    {
    if ($this->errno == CURLE_OPERATION_TIMEOUTED)
        $this->result = true;
    else
        $this->result = false;
        $this->errmsg = curl_error($curl);
    }
    else
    {
        $this->resp_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $this->result = true;
    }
        Log::info("get response: [".$this->errno."] [".$this->response."]");
        curl_close($curl);
    }

    private function checkSSL($url)
    {
        $protocol = strtolower(substr($url, 0, 5));
        if ($protocol == "https")
        return true;
        else
        return false;
    }

    private function init()
    {
        $this->result = false;
        $this->errno = 0;
        $this->errmsg = "";
        $this->response = "";
        $this->resp_code = 0;
    }


    public function post($url, $postdata, $timeout)
    {
        $ssl = $this->checkSSL($url);

        $this->init();
        Log::info("post request: [$url] [$postdata]");


        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);

        if ($ssl)
        {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        }
        Log::info("header: ".json_encode($this->header));

        if (!empty($this->header))
        curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);

        if ($this->cacert) {
        curl_setopt($curl, CURLOPT_CAINFO, $this->cacert);
        }

        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);

        $this->result = false;
        $this->response = curl_exec($curl);
        $this->errno = curl_errno($curl);
        if ($this->errno > 0)
        {
        if ($this->errno == CURLE_OPERATION_TIMEDOUT)
            $this->result = true;
        else
            $this->result = false;
            $this->errmsg = curl_error($curl);
        }
        else
        {
            $this->resp_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            $this->result = true;
        }
        Log::info("curl_http post response: [".$this->errno."] [".$this->response."]");
            curl_close($curl);
        }
    }